/***********************************************
 This file is part of redia-lib-utils-ios
.
 
 Copyright (c) 2012, 2013 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */


#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <sqlite3.h>
#import "UrlCacheImageEntry.h"
#import "UrlCacheVideoEntry.h"
#import "UrlCacheAudioEntry.h"
#import "ProgressDisplayDelegate.h"

typedef enum : NSUInteger {
    UrlCacheManagerFileTypeUnknown,
    UrlCacheManagerFileTypeImage,
    UrlCacheManagerFileTypeVideo,
    UrlCacheManagerFileTypeAudio

} UrlCacheManagerFileType;


@interface UrlCacheManager : NSObject {
	NSString *databasePath;
	NSString *cacheFilesPath;
	
	int defaultCacheFileExpireTimeSeconds;

	sqlite3	*cacheDB;
	BOOL isOffLineMode;
}

@property (nonatomic, copy) NSString *databasePath;
@property (nonatomic, copy) NSString *cacheFilesPath;
@property (nonatomic) BOOL isOffLineMode;
@property (nonatomic) int defaultCacheFileExpireTimeSeconds;

+ (UrlCacheManager*) instance;

- (void)setupDB;

- (void) saveImage:(NSData*)imageData fromUrl:(NSURL*)url imageWidth:(int)width imageHeight:(int)height;
- (void) saveImage:(NSData*)imageData fromUrl:(NSURL*)url imageWidth:(int)width imageHeight:(int)height expireTime:(NSTimeInterval)expireUnixTime;

- (UrlCacheImageEntry*) loadImageUrl:(NSURL*)url delegate:(id<UrlCacheEntryDelegate>)delegate;
- (UrlCacheImageEntry*) loadImageUrl:(NSURL*)url delegate:(id<UrlCacheEntryDelegate>)delegate doDownloadOnly:(BOOL)download_only;

- (NSString*) getLocalCacheFilePath;
- (NSURL*) getLocalCacheFileBaseUrl;

/*! Method for looking up efternal resources in local cache
 \param url A URL for an external resource
 \param fileType File type of the external resource
 \returns Either a URL for a cached version of the resource. Or the input URL, if no cached version exists
 \discussion This method is intended for Audio and Video resources.
 */
-(NSURL*)getCacheURLforURL:(NSURL*)url withFileType:(UrlCacheManagerFileType)fileType;

- (NSString*)getLocalCacheFilePathForUrl:(NSURL*)url withFileType:(UrlCacheManagerFileType)fileType;

- (NSString*)getLocalCacheFileNameForUrl:(NSURL*)url withFileType:(UrlCacheManagerFileType)fileType;

- (NSString *)getLocalCacheFileNameForImageUrl:(NSURL *)url size:(CGSize*)out_size;
- (NSString *)getLocalCacheFileNameForImageUrl:(NSURL *)url size:(CGSize*)out_size initiateDownload:(BOOL)do_download;

- (NSString*) saveVideoFromTempFilePath:(NSString*)tempFilePath fromUrl:(NSURL*)url expireTime:(NSTimeInterval)expireUnixTime;
- (UrlCacheVideoEntry*)loadVideoUrl:(NSURL*)url delegate:(id<UrlCacheEntryDelegate>)delegate;
- (NSString *)getLocalCacheFileNameForVideoUrl:(NSURL *)url size:(CGSize*)out_size initiateDownload:(BOOL)do_download downloadProgressDelegate:(id<ProgressDisplayDelegate>)progressDelegate;

- (NSString*) saveAudioFromTempFilePath:(NSString*)tempFilePath fromUrl:(NSURL*)url expireTime:(NSTimeInterval)expireUnixTime;
- (UrlCacheAudioEntry*)loadAudioUrl:(NSURL*)url delegate:(id<UrlCacheEntryDelegate>)delegate;
- (NSString *)getLocalCacheFileNameForAudioUrl:(NSURL *)url size:(CGSize*)out_size initiateDownload:(BOOL)do_download downloadProgressDelegate:(id<ProgressDisplayDelegate>)progressDelegate;



- (void) removeCacheFile:(NSString*) filename;
- (void) removeEntry:(sqlite_int64)row_id; 

- (BOOL)checkFilenameInDB:(NSString*)filename;
- (void)checkConsistency;
- (void)checkConsistencyWithProgressDelegate:(id<ProgressDisplayDelegate>)_delegate;

- (void)clearEntireCacheDatabase;


@end
